package ${entityControllerPackage};

import java.util.List;
import java.util.stream.Collectors;
import javax.validation.constraints.NotBlank;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rock.microshop.common.SequenceService;
import com.rock.microshop.common_api.dto.PageDto;
import com.rock.microshop.common_api.vo.PageVo;
import chianghao.core.mybatis.plugins.Paging;
import chianghao.core.web.R;
import chianghao.core.web.ValidatedGroup;


import ${entityDtoClassName};
import ${entityVoClassName};
import ${entityClassName};
import ${entityServiceClassName};


@RestController
@RequestMapping("${controllerRootPath}")
public class ${entityClassSimpleName}Controller {

    @Autowired
    ${entityServiceClassSimpleName} ${entityServiceObjectName};

    @Autowired
    SequenceService sequenceService;

	@PostMapping("add")
	public R<Boolean> add(@Validated({ValidatedGroup.Add.class}) ${entityClassSimpleName}Dto dto) {
		${entityClassSimpleName} ${entityObjectName} = toEntity(dto);
		Long code  = sequenceService.getNextId();
		${entityObjectName}.setCode(code);
		${entityServiceObjectName}.insert(${entityObjectName});
		return R.getSuccess(true);
	}

    @PostMapping("modify")
    public R<Boolean> modify(@Validated({ValidatedGroup.Modify.class}) ${entityClassSimpleName}Dto dto) {
    	${entityClassSimpleName} ${entityObjectName} = toEntity(dto);
		${entityServiceObjectName}.update(${entityObjectName});
    	return R.getSuccess(true);
	}

    @DeleteMapping("delete")
    public R<Boolean> delete(@NotBlank(message="error:code,not.allow.null") Long code) {
    	${entityServiceObjectName}.deleteByCode(code);
    	return R.getSuccess(true);
    }

    @GetMapping("detail")
    public R<${entityClassSimpleName}Vo> detail(@NotBlank(message="error:code,not.allow.null") Long code) {
    	${entityClassSimpleName}  bean = ${entityServiceObjectName}.queryOneByCode(code);
    	return R.getSuccess(toVo(bean));
    }

    @GetMapping("query")
    public R<List<${entityClassSimpleName}Vo>> query(${entityClassSimpleName}Dto dto) {
    	List<${entityClassSimpleName}> List = ${entityServiceObjectName}.query(toEntity(dto));
    	return R.getSuccess(toListVo(List));
    }

    @GetMapping("page-query")
    public R<PageVo<${entityClassSimpleName}Vo>> queryPageList(${entityClassSimpleName}Dto dto, PageDto page) {
    	Paging paging  = new Paging(page.getCurrent(),page.getPageSize()); 
    	List<${entityClassSimpleName}> List = ${entityServiceObjectName}.queryPage(toEntity(dto),paging );
    	PageVo<${entityClassSimpleName}Vo> pageVo = new PageVo<${entityClassSimpleName}Vo>();
    	pageVo.setItems(toListVo(List));
    	pageVo.setTotal(paging.getTotalRows());
    	return R.getSuccess(pageVo);
    }
    
    
    private ${entityClassSimpleName}Vo toVo(${entityClassSimpleName} bean) {
    	${entityClassSimpleName}Vo vo = new ${entityClassSimpleName}Vo();
    	BeanUtils.copyProperties(bean, vo);
    	return vo;
    }
    
    private List<${entityClassSimpleName}Vo> toListVo(List<${entityClassSimpleName}> list){
    	List<${entityClassSimpleName}Vo> array = list.stream().map(item->toVo(item)).collect(Collectors.toList());
    	return array;
    }

    private ${entityClassSimpleName} toEntity(${entityClassSimpleName}Dto dto) {
    	${entityClassSimpleName} bean = new ${entityClassSimpleName}();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }

}